// pages/phonebillPurchase/phonebillPurchase.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    currentCouId:'',
    userCouId:'',
    pageTitle: '' ,
    products:[
      
    ],
    buyBtnDisabled:false,
  },
  //获取商品列表
  getProduct:function(){
    let _this=this;
    let giftType=1
    if(this.data.id==13){
      giftType=1
    }
    if(this.data.id==14){
      giftType=2
    }
    if(this.data.id==15){
      giftType=3
    }
    if(this.data.id==16){
      giftType=4
    }
    wx.request({
      url:app.globalData.onlineUrl+"/productNew/listByType",
      header:{'content-type': 'application/json'},
      method:"POST",
      data:{giftType:giftType},
      dataType:"json",
      complete:function(result) {
        console.log(9999,result.data.data);
        let data=result.data.data;
        _this.setData({
          products:data,
        })
      }
    })
  },
  generateOrderId() {
    return this.getOrderNoFunc("yyyyMMddhhmmssS")+Math.floor(Math.random() * 89) + 10; // 组合时间戳和随机数
  },
  settlementSale:function(event){
    let id=event.currentTarget.dataset.id;
    //获取商品详情
    let _this=this;
    wx.request({
      url:app.globalData.onlineUrl+"/productNew/detailById",
      header:{'content-type': 'application/json'},
      method:"POST",
      data:{id:id},
      dataType:"json",
      success:function(result) {
        console.log(1234454,result.data.data[0]);
        const items ={
          totalPrice:result.data.data[0].price,
          totalOriPrice:result.data.data[0].orinprice,
          orinDiscount:(result.data.data[0].orinprice-result.data.data[0].price).toFixed(2),
          items:[
            {
              buyNum:1,
              discountType:result.data.data[0].discount_type,
              id: '',
              imgUrl: result.data.data[0].imgUrl,
              inventory: result.data.data[0].inventory,
              isSelected:1,
              iscoupon: result.data.data[0].iscoupon,
              orinprice: result.data.data[0].orinprice,
              price: result.data.data[0].price,
              productId: result.data.data[0].id,
              skuName:result.data.data[0].skuName,
              state: result.data.data[0].state,
              type: result.data.data[0].type,
            }
          ]
        }
        console.log(123,items)
        wx.navigateTo({
          url: '/pages/settlementSale/settlementSale?item='+JSON.stringify(items)+'&currentCouId='+_this.data.currentCouId+'&userCouId='+_this.data.userCouId+'&typeId='+_this.data.id,

        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id
    let currentCouId=options.currentCouId
    let userCouId = options.userCouId
    let pageTitle = ''
    if(id==13){
      pageTitle = '29元专区'
    }else if(id==14){
      pageTitle = '59元专区'
    }else if(id==15){
      pageTitle = '69元专区'
    }else if(id==16){
      pageTitle = '99元专区'

    }
    this.setData({
      id:id,
      pageTitle:pageTitle,
      currentCouId:currentCouId,
      userCouId:userCouId,
    })
    this.getProduct();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //需要校验当前有没有对应的换购券，如果没有换购按钮要禁用
    //校验当前换购券是否过期
    let id = this.data.id
    let _this=this;
    wx.request({
      url:app.globalData.onlineUrl+"/couponsCountByStatus",
      header:{'content-type': 'application/json'},
      method:"POST",
      data:{
        phone:wx.getStorageSync('phone'),
        discountType:id,
      },
      dataType:"json",
      success:function(result) {
        console.log(123123,result.data.data);
        _this.setData({
          buyBtnDisabled:result.data.data.count==0,
        })
      }
    })
    this.getProduct();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})